﻿using MHI.ScenarioManager.Interfaces;
using WebFoundation.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperExtensions;

namespace MHI.ScenarioManager.Data
{
    internal class ScenarioQueryDAO : QueriableDAO<Scenario>
    {
        protected static DbManager _dbManager = WebFoundation.Data.DbManager.FromConfig("DefaultConnection");

        public ScenarioQueryDAO()
        : base(_dbManager)
        {
        }  

        public ScenarioQueryDAO(DbManager db)
            : base(db)
        {
        } 

        public Scenario GetScenarioByCode(string scenarioCode)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.ScenarioCode, scenarioCode);
            query.OrderBy(r => r.CreateTime);
            IEnumerable<Scenario> scenarioList = this.Query(query);
            if (scenarioList != null && scenarioList.Count() > 0)
            {
                return scenarioList.ElementAt(0);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据模型类型获取方案，排除模板方案
        /// </summary>
        /// <param name="modelType"></param>
        /// <returns></returns>
        public IEnumerable<Scenario> GetScenarioByModelType(enumScenarioType modelType)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.ModelType, modelType.ToString()).And().Eq(r => r.Template, 0);
            query.OrderBy(r => r.CreateTime);
            return this.Query(query);
        }

        /// <summary>
        /// 根据模型子类获取方案集合，排除模板方案
        /// </summary>
        /// <param name="modelType"></param>
        /// <returns></returns>
        public IEnumerable<Scenario> GetScenarioByModelSubType(string modelSubType)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.ModelSubType, modelSubType.ToString()).And().Eq(r => r.Template, 0);
            query.OrderBy(r => r.CreateTime);
            return this.Query(query);
        }

        public IEnumerable<Scenario> GetChildScenarioByScenario(Guid scenarioId)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.InheritedScenario, scenarioId).And().Eq(r => r.Template, 0);
            query.OrderBy(r => r.CreateTime);
            return this.Query(query);
        }

        /// <summary>
        /// 获取所有的模板方案
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Scenario> GetTemplateScenarios()
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.Template, 1);
            query.OrderBy(r => r.CreateTime);
            return this.Query(query);
        }

        /// <summary>
        /// 根据方案CODE获得唯一的方案CODE
        /// </summary>
        /// <param name="newScenarioCode"></param>
        /// <returns></returns>
        public string GetNewUniqueScenarioCode(string newScenarioCode)
        {
            Scenario scenario = null;
            string returnData = newScenarioCode;
            int i = 1;
            scenario = _GetUniqueScenarioCode(newScenarioCode);
            while (scenario != null)
            {
                returnData = newScenarioCode + i.ToString();
                scenario = _GetUniqueScenarioCode(returnData);
                i += 1;
            }
            return returnData;
        }

        public Scenario _GetUniqueScenarioCode(string newScenarioCode)
        {
            var query = this.CreateQuery();
            query.Filter().Eq(r => r.ScenarioCode, newScenarioCode);
            IEnumerable<Scenario> scenarioList = this.Query(query);
            if (scenarioList != null && scenarioList.Count() > 0)
            {
                return scenarioList.ElementAt(0);
            }
            else
            {
                return null;
            }
        }
    }
}
